﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "教师课表");
		
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		/* 设置为横向打印 */
		event.setArg("portrait", false);
		
		termId = event.getArg("TID");
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		teacherId = event.getArg("TCHID");
	
		sql = "SELECT a.tch_name,b.institute_name,
					   a.tch_id 
				FROM t_teacher a 
					 INNER JOIN t_institute b ON b.institute_id = a.institute_id 
				WHERE a.tch_id = :teacherId ";
				
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
		
		rs_teacher = queryObj.execute( sql=sql ).getResult();
		
		/* 读取教师课程的排课记录 */
		
		sql = "SELECT 
					b.course_name, 
					a.tasktime , 
					case when a.taskclsroom is null then '-' else a.taskclsroom end taskclsroom, 
					a.task_week, 
					a.task_type, a.task_id  
				FROM t_teacher_timetable a 
					INNER JOIN t_course b ON b.cid =a.cid 
				WHERE 
					a.tch_id = :teacherId 
					AND 
					a.term_id = :termId "; 
					
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		
		rs_tableData = queryObj.execute( sql=sql ).getResult();
		
		timeTable = arrayNew(2);
		
		/* 课表冲突标记 */
		for ( lec=1; lec lte 6; lec++ ) {
			for ( wkday=1; wkday lte 7; wkday++ ) {
				timeTable[wkday][lec] = 0;
			}
		}
		
		timeTableSet = structNew();
		
		/* 生成课表显示数据 */
		for ( i=1; i LTE rs_tableData.recordCount; i++) {
			
			currentTaskTime = rs_tableData["tasktime"][i];
			
			if ( rs_tableData["taskclsroom"][i] eq "-" ) { 
				currentTaskPlace = "";
			}
			else {
				currentTaskPlace = rs_tableData["taskclsroom"][i];
			}
			
			wkday = listGetAt(currentTaskTime, 1, "-@");
			lec = listGetAt(currentTaskTime, 2, "-@");
			cell = listGetAt(currentTaskTime, 3, "-@");
			
			/* 载入教学任务 */
			switch( rs_tableData["task_type"][i] ){
				case "C":
					sql = "SELECT 
								 tch.tch_name tch_name, tch_sec.tch_name tch_name_sec, tch_ace.tch_name tch_name_ace 
							FROM 
								 t_common_task  
								 INNER JOIN t_teacher tch ON tch.tch_id = t_common_task.tch_id 
								 INNER JOIN t_teacher tch_sec ON tch_sec.tch_id = t_common_task.tch_id_sec 
								 INNER JOIN t_teacher tch_ace ON tch_ace.tch_id = t_common_task.tch_id_ace 
							WHERE 
								 t_common_task.tsk_id = :taskId ";
					break;
	
				case "P":
					sql = "SELECT 
								 tch.tch_name tch_name, tch_sec.tch_name tch_name_sec, tch_ace.tch_name tch_name_ace 
							FROM 
								 t_task  
								 INNER JOIN t_teacher tch ON tch.tch_id = t_task.tch_id 
								 INNER JOIN t_teacher tch_sec ON tch_sec.tch_id = t_task.tch_id_sec 
								 INNER JOIN t_teacher tch_ace ON tch_ace.tch_id = t_task.tch_id_ace 
							WHERE 
								 t_task.tsk_id = :taskId ";
					break;
				
				case "R":
					sql = "SELECT 
								 tch.tch_name tch_name, tch_sec.tch_name tch_name_sec, tch_ace.tch_name tch_name_ace 
							FROM 
								 t_reopen_task  
								 INNER JOIN t_teacher tch ON tch.tch_id = t_reopen_task.tch_id 
								 INNER JOIN t_teacher tch_sec ON tch_sec.tch_id = t_reopen_task.tch_id_sec 
								 INNER JOIN t_teacher tch_ace ON tch_ace.tch_id = t_reopen_task.tch_id_ace 
							WHERE 
								 t_reopen_task.tsk_id = :taskId ";
					break;
				
				case "T":
					sql = "SELECT 
								 tch.tch_name tch_name, tch_sec.tch_name tch_name_sec, tch_ace.tch_name tch_name_ace 
							FROM 
								 t_sport_task  
								 INNER JOIN t_teacher tch ON tch.tch_id = t_sport_task.tch_id 
								 INNER JOIN t_teacher tch_sec ON tch_sec.tch_id = t_sport_task.tch_id_sec 
								 INNER JOIN t_teacher tch_ace ON tch_ace.tch_id = t_sport_task.tch_id_ace 
							WHERE 
								 t_sport_task.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=rs_tableData["task_id"][i], cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			
			/* 生成教师 */
			currentTeacher = rs_task.tch_name;
			
			if ( not listFind( currentTeacher, rs_task.tch_name_sec, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_task.tch_name_sec;
			}
			
			if ( not listFind( currentTeacher, rs_task.tch_name_ace, "/ " ) ) {
				currentTeacher = currentTeacher & " / " & rs_task.tch_name_ace;
			}
			
			/* 
				生成课表内容 
				cellCourse = [ courseName, teacherName, place, cellLength, taskWeek]
			*/
			if ( cell eq 2 ) {

				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, cell, rs_tableData["task_week"][i] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
		
			}
			
			if ( cell eq 3 and lec eq 2 ) {

				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, cell, rs_tableData["task_week"][i] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
					
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			if ( cell eq 3 and lec eq 5 ) {

				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 2, rs_tableData["task_week"][i] ];
			
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
		
				/* 晚上3节延续1学时 */
				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 1, rs_tableData["task_week"][i] ];
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}

					arrayAppend( timeTableSet[lectureKey], cellCourse );
			
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
			}
			
			/* 4学时延续 */
			if ( cell eq 4 ) {
				
				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 2, rs_tableData["task_week"][i] ];
		
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
			
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
			
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
					}
			
			/* 5学时延续 */
			if ( cell eq 5 ) {
				
				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 2, rs_tableData["task_week"][i] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 3, rs_tableData["task_week"][i] ];
				
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			}
			
			/* 8学时延续 */
			if ( cell eq 8 ) {
				
				cellCourse = [ rs_tableData["course_name"][i], currentTeacher, currentTaskPlace, 2, rs_tableData["task_week"][i] ];
				
				lectureKey = "KB" & wkday & lec;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}
			
				lectureKey = "KB" & wkday & lec+1;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+1] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+2;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+2] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

				lectureKey = "KB" & wkday & lec+3;
				if ( structKeyExists( timeTableSet, lectureKey ) ) {
					
					/* 计算课表单元时间冲突 */
					for ( cellCourseExists in timeTableSet[lectureKey] ) {
						if ( mathAdvice.isWeekOver( cellCourseExists[5], cellCourse[5] ) ) {
							/* 同一单元格 如果上课周次有重叠 */
							timeTable[wkday][lec+3] = 1;
						}
					}
					
					arrayAppend( timeTableSet[lectureKey], cellCourse );
				}
				else {
					structInsert( timeTableSet, lectureKey, [cellCourse] );
				}

			}

		}
		
	</cfscript>
</cfsilent>

<!--- /* 单个教师课表 */ --->
<cfset tagCode = "JXRYKB-" & termId & "," & teacherId />

<div class="UIprintableTitle"><cfoutput>#academicYear#</cfoutput>-<cfoutput>#academicTerm#</cfoutput>学期 教师课表 <cfoutput>#rs_teacher.tch_name#</cfoutput></div>
<table width="98%" border="0" cellpadding="1" cellspacing="0">
	<tr>
		<td>所在部门: <cfoutput>#rs_teacher.institute_name#</cfoutput></td>
		<td align="right">任课教师: <cfoutput>#rs_teacher.tch_name#</cfoutput> (<cfoutput>#rs_teacher.tch_id#</cfoutput>)</td>
	</tr>
</table>

<!-- courseTable -->
<table class="UIprintable noborder" width="100%" border="1" cellpadding="3" cellspacing="1">
	<thead>
		<tr class="noborder">
			<td colspan="2" width="2%"></td>
			<td width="14%">星期一</td>
			<td width="14%">星期二</td>
			<td width="14%">星期三</td>
			<td width="14%">星期四</td>
			<td width="14%">星期五</td>
			<td width="14%">星期六</td>
			<td width="14%">星期天</td>
		</tr>
	</thead>
	<tbody>
		<cfloop from="1" to="6" index="lec">
			<tr>
				<cfswitch expression="#lec#">
					<cfcase value="1">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="3">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
					<cfcase value="5">
					<td rowspan="2"><cfoutput>#mathAdvice.convertLectureIdxToDayPart(lec)#</cfoutput></td>
					</cfcase>
				</cfswitch>
				<td><cfoutput>#mathAdvice.convertLectureIdxToChinese(lec)#</cfoutput></td>
				<cfloop from="1" to="7" index="wkday">
					<cfset lectureKey = "KB" & wkday & lec />
					<td class="timeCell <cfif timeTable[wkday][lec]>attention</cfif>">
						<cfif structKeyExists( timeTableSet, lectureKey )>
							<cfset i = 0 />
							<cfloop array="#timeTableSet[lectureKey]#" index="cellCourse">
								<cfset i++ />
								<div class="lecture"> 
									<span class="course"><cfoutput>#cellCourse[1]#</cfoutput></span> - <span class="teacher"><cfoutput>#cellCourse[2]#</cfoutput></span> 
									<br/>
									<span class="week"><cfoutput>#cellCourse[5]#</cfoutput>(<cfoutput>#cellCourse[4]#</cfoutput>)</span>
									<cfif cellCourse[3] neq "-">
										<br/>
										<span class="place"><cfoutput>#cellCourse[3]#</cfoutput></span>
									</cfif>
								</div>
								<cfif i lt arrayLen(timeTableSet[lectureKey]) >
									<div class="cellBreaker"><!--.--></div>
								</cfif>
							</cfloop>
						</cfif>
					</td>
				</cfloop>
			</tr>
		</cfloop>
	</tbody>
</table>

<div class="clear2px"></div>
<table width="100%" cellpadding="0" cellspacing="0">
	<tr>
		<td>打印时间 <cfoutput>#dateFormat(now(), "YYYY年MM月DD日")#</cfoutput></td>
		<td align="right"><img src="<cfoutput>#getContextRoot()#</cfoutput>/barcode/Print/<cfoutput>#tagCode#</cfoutput>.png" height="43"/></td>
	</tr>
</table>
